const { default: axios } = require("axios");
const cron = require("node-cron");
const mailer = require("nodemailer");
const fs = require("fs");

const url = "https://m.nuaa.edu.cn/ncov/wap/default/save";

const body = {
  sfzhux: 1,
  zhuxdz: "",
  szgj: "",
  szcs: "",
  szgjcs: "",
  sfjwfh: 0,
  sfyjsjwfh: 0,
  sfjcjwfh: 0,
  sflznjcjwfh: 0,
  sflqjkm: 4,
  jkmys: 0,
  sfjtgfxdq: 0,
  nuaaxgymjzqk: 1,
  dyzjzsj: "",
  bfhzjyq: 0,
  hxyxjzap: 0,
  yjzjsj: "",
  sftjlkjc: "",
  lkjctlsj: "",
  sfsylkjcss: "",
  gjzsftjlkjc: "",
  gjzlkjctlsj: "",
  gjzsfsylkjcss: "",
  ifhxjc: "",
  hsjconetime: "",
  hsjconeplace: "",
  hsjconejg: "",
  hsjctwotime: "",
  hsjctwoplace: "",
  hsjctwojg: "",
  hsjcthreetime: "",
  hsjcthreeplace: "",
  hsjcthreejg: "",
  hsjcfourtime: "",
  hsjcfourplace: "",
  hsjcfourjg: "",
  ywchxjctime: "",
  hsjclist: {},
  njrddz: "B11",
  gzczxq: 1,
  ifznqgfxljs: "",
  iflsqgfxljs: "",
  zrwjtw: 1,
  jrzjtw: 1,
  jrlvymjrq: "",
  ifcyglq: 0,
  wskmyy: "",
  zhycjgdqifjn: "",
  dqsfzgfxszqs: 0,
  gqsfyzgfxljs: 0,
  gqsfyqzfhryjc: 0,
  sfyjwljqyhg: 0,
  id: 21466980,
  uid: 263245,
  date: 20211202,
  tw: "",
  sfcxtz: 0,
  sfyyjc: 0,
  jcjgqr: 0,
  jcjg: "",
  sfjcbh: 0,
  sfcxzysx: 0,
  qksm: "",
  remark: "",
  address: "江苏省南京市秦淮区瑞金路街道李府街南京航空航天大学明故宫校区",
  area: "江苏省 南京市 秦淮区",
  province: "江苏省",
  city: "南京市",
  geo_api_info: {
    type: "complete",
    position: {
      Q: 32.033498806424,
      R: 118.82297444661504,
      lng: 118.822974,
      lat: 32.033499,
    },
    location_type: "html5",
    message: "Get geolocation success.Convert Success.Get address success.",
    accuracy: 65,
    isConverted: true,
    status: 1,
    addressComponent: {
      citycode: "025",
      adcode: "320104",
      businessAreas: [
        {
          name: "月牙湖",
          id: "320104",
          location: {
            Q: 32.03049,
            R: 118.829948,
            lng: 118.829948,
            lat: 32.03049,
          },
        },
        {
          name: "苜蓿园",
          id: "320104",
          location: {
            Q: 32.02916,
            R: 118.83327700000001,
            lng: 118.833277,
            lat: 32.02916,
          },
        },
        {
          name: "大光路",
          id: "320104",
          location: {
            Q: 32.025771,
            R: 118.80886599999997,
            lng: 118.808866,
            lat: 32.025771,
          },
        },
      ],
      neighborhoodType: "",
      neighborhood: "",
      building: "",
      buildingType: "",
      street: "李府街",
      streetNumber: "4-13号",
      country: "中国",
      province: "江苏省",
      city: "南京市",
      district: "秦淮区",
      township: "瑞金路街道",
    },
    formattedAddress: "江苏省南京市秦淮区瑞金路街道李府街南京航空航天大学明故宫校区",
    roads: [],
    crosses: [],
    pois: [],
    info: "SUCCESS",
  },
  created: 1638428827,
  sfzx: 1,
  sfjcwhry: 0,
  sfcyglq: 0,
  gllx: "",
  glksrq: "",
  jcbhlx: "",
  jcbhrq: "",
  sftjwh: 0,
  sftjhb: 0,
  fxyy: "",
  bztcyy: "",
  fjsj: 0,
  created_uid: 0,
  sfjchbry: 0,
  sfjcqz: "",
  jcqzrq: "",
  jcwhryfs: "",
  jchbryfs: "",
  xjzd: "",
  sfsfbh: 0,
  sfjcwzry: 0,
  sftjwz: 0,
  jhfjrq: "",
  jhfjjtgj: "",
  jhfjhbcc: "",
  jhfjsftjwh: 0,
  jhfjsftjhb: 0,
  szsqsfybl: 0,
  sfygtjzzfj: 0,
  gtjzzfjsj: "",
  sfsqhzjkk: 0,
  sqhzjkkys: "",
  is_fx_log: 0,
  fxzrwjtw: 0,
  fxjrcjtw: 0,
  fxjrzjtw: 0,
  fxsfzx: 0,
  fxsfcyglq: 0,
  fxsfcxtz: 0,
  sfjzxg: 0,
  skmcolor: 1,
  skmimg: "",
  ifjrglkjc: "",
  gtjjsfhm: "",
  gtjzsfhzl: "",
  sffhddjjgc: "",
  ifjgzgfxq: "",
  jgzgfxrq: "",
  jgzgfxdq: "",
  jgzgfxxxdz: "",
  newwchsjccs: "",
  dsdecjcsj: "",
  dsdechsjcjgtype: "",
  dsdrchsjcdd: "",
  dsdechsjcjg: "",
  zhyccjcsj: "",
  zhycchsjcjgtype: "",
  zhycchsjcdd: "",
  zhycchsjcjg: "",
  gwszdd: "",
  sfyqjzgc: "",
  jrsfqzys: "",
  jrsfqzfy: "",
  ismoved: 0,
};

const transFun = (data = {}) =>
  Object.entries(data)
    .map((ent) => ent.join("="))
    .join("&");

const config = {
  transformRequest: transFun,
  headers: {
    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
    Cookie: "",
  },
};

const getDate = function () {
  let date = new Date();
  date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
  return parseInt(date.toJSON().substr(0, 11).replace(/[-T]/g, ""));
};

cron.schedule("0 0 8 * * *", () => {
  let users = JSON.parse(fs.readFileSync("./users.json").toString());
  users.forEach((user) => {
    body.id = 21466980;
    body.uid = 263245;
    body.created = Math.round(new Date().getTime() / 1000);
    body.date = getDate();
    config.headers.Cookie = user.Cookie;
    axios
      .post(url, body, config)
      .then(function (response) {
        if (response.data.m === "操作成功") {
          console.log("[SUCCESS]:", Date(), response.data.m, "user: " + user.name);
        } else {
          throw new Error();
        }
      })
      .catch(function (error) {
        console.log("[ERROR]:", Date(), error, "user: " + user.name);
        let transproter = mailer.createTransport({
          service: "qqex",
          secure: true,
          auth: {
            user: "imcao@imcao.cn",
            pass: "ekFr57wonC6XdKh5",
          },
        });
        let options = {
          from: "imcao@imcao.cn",
          to: "imcao@imcao.cn",
          subject: `【${getDate()}】打卡失败`,
          text: `${user.name}打卡失败，请手动打卡并修改cookie！`,
        };
        transproter.sendMail(options, (err, info) => {
          if (err) {
            console.log(err, info);
          }
        });
      });
  });
});
